package com.xiaoxu.rental.security;

import cn.hutool.core.io.IoUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.xiaoxu.rental.util.Result;
import com.xiaoxu.rental.util.ResultCode;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Title: LoginFailedHandler
 * @Author Mr.Xu
 * @Package com.xiaoxu.rental.security
 * @Date 2024/7/25 15:01
 * @description: 登录失败的处理器
 */
@Component
public class LoginFailedHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
                                        HttpServletResponse response,
                                        AuthenticationException exception) throws IOException, ServletException {
        // 设置响应内容类型为JSON，编码方式为UTF-8
        response.setContentType("application/json;charset=utf-8");
        ServletOutputStream outputStream = response.getOutputStream();

        //初始化错误代码和消息
        // 初始化默认的错误代码和消息
        int code= ResultCode.ERROR;
        String msg=null;

        // 根据不同的异常类型设置不同的错误代码和消息
        if (exception instanceof AccountExpiredException){
            code=ResultCode.UNAUTHENTICATED;
            msg="账户过期";
        }else if (exception instanceof BadCredentialsException){
            code=ResultCode.UNAUTHENTICATED;
            msg="账户或密码错误";
        }else if (exception instanceof CredentialsExpiredException){
            code=ResultCode.UNAUTHENTICATED;
            msg="密码过期";
        }else if (exception instanceof DisabledException){
            code=ResultCode.UNAUTHORIZED;
            msg="账户被禁用";
        }else if (exception instanceof LockedException){
            code=ResultCode.UNAUTHORIZED;
            msg="账户被锁定";
        }else if (exception instanceof InternalAuthenticationServiceException){
            code=ResultCode.UNAUTHENTICATED;
            msg="账户不存在";
        }else if (exception instanceof CustomerAuthenticationException){
            code=ResultCode.UNAUTHORIZED;
            msg=exception.getMessage();
        }
        else {
            msg="登录失败";
        }

        // 使用JSON格式化错误信息并写入响应体
        String result= JSON.toJSONString(Result.fail().setCode(code).setMessage(msg));
//        outputStream.write(result.getBytes(StandardCharsets.UTF_8));
//        outputStream.flush();
//        outputStream.close();
         //使用hutool构建错误信息
//        String result = JSONUtil.toJsonStr(Result.fail().setCode(code).setMessage(msg));
//
//        // 使用hutool的IoUtil工具类来写入响应体
        IoUtil.write(response.getOutputStream(), false, result.getBytes());


    }
}
